//
//  SWProgressView.m
//  DataTransfer
//
//  Created by 宋润理 on 2023/12/20.
//  Copyright © 2023 James. All rights reserved.
//

#import "SWProgressView.h"

@interface SWProgressView ()

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) CancelBlock cancelBlock;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UIButton *cancelBtn;

@end

@implementation SWProgressView

#pragma mark - public

- (instancetype)initWithTitle:(NSString *)title andCancelBlock:(CancelBlock)cancelBlock
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = UIColorFromHexA(0x000000, 0.5);
        _title = title;
        _cancelBlock = cancelBlock;
        _progress = 0.0f;
        [self displayUI];
    }
    return self;
}

- (void)showWithDefaultProgress {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    self.progress = 0.0;
}

- (void)dismiss {
    [self removeFromSuperview];
}

#pragma mark - actions
- (void)onClickCancel {
    if (_cancelBlock) {
        _cancelBlock();
    }
    [self dismiss];
}

#pragma mark - private
- (void)displayUI {
    [self addSubview:self.contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@(0));
        make.left.mas_equalTo(50);
        make.right.mas_equalTo(-50);
        make.height.equalTo(@(300));
    }];
    
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@(0));
    }];
    
    [self.contentView addSubview:self.progressView];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(25));
        make.right.equalTo(@(-25));
        make.top.equalTo(self.titleLabel.mas_bottom).offset(25);
    }];
    
    [self.contentView addSubview:self.progressLabel];
    [_progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.progressView);
        make.top.equalTo(self.progressView.mas_bottom).offset(25);
    }];
    
    [self.contentView addSubview:self.cancelBtn];
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.progressView);
        make.bottom.equalTo(@(-25));
        make.height.equalTo(@(40));
    }];
}

#pragma mark - setter
- (void)setProgress:(float)progress {
    _progress = progress;
    [self.progressView setProgress:_progress animated:YES];
}

- (void)setProgressText:(NSString *)progressText {
    _progressText = progressText;
    self.progressLabel.text = _progressText;
}

#pragma mark - getter
- (UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = UIColor.whiteColor;
        _contentView.layer.cornerRadius = 4;
        _contentView.clipsToBounds = YES;
    }
    return _contentView;
}

- (UIProgressView *)progressView{
    if(!_progressView){
        _progressView = [[UIProgressView alloc] init];
    }
    return _progressView;
}

- (UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)progressLabel{
    if(!_progressLabel){
        _progressLabel = [[UILabel alloc] init];
        _progressLabel.font = [UIFont systemFontOfSize:14];
        _progressLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _progressLabel;
}

- (UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.backgroundColor = MainThemeColor;
        [_cancelBtn setTitle:@"取消传输" forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(onClickCancel) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

@end
